package sany.iec.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import sany.iec.common.GlobalConstant;
import sany.iec.service.ems.EMSDataService;
import sany.iec.service.ems.EmsServerServiceImpl;
import sany.iec.service.olp01.OLP01DataService;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * @Author : 托马斯
 * @Date : 2025/7/2 10:05
 * @Description :
 */
@RestController
@RequestMapping("/ems")
@Slf4j
public class IECController {

    @Resource
    private EmsServerServiceImpl emsServerService;

    @Resource
    private EMSDataService emsDataService;

    @Resource
    private OLP01DataService olpDataService;

    @GetMapping("/value")
    public Object value() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("iec_receive", GlobalConstant.RECEIVE_DATA);
        map.put("iec_yx_yc", emsDataService.getIEC_DATA());
        map.put("olp_yx_yc", olpDataService.getIEC_DATA());
        map.put("rate", GlobalConstant.RATE);
        map.put("voltage", GlobalConstant.RATED_VOLTAGE);
        map.put("SVG_AVC", GlobalConstant.SVG_VALUE);
        return map;
    }

    @GetMapping("/startorstop")
    public Object startOrStopEms(@RequestParam("flag") Integer flag) throws IOException {
        if (flag == null || flag == 0) {
            if (!emsServerService.getServer().isStopped()) {
                emsServerService.stopEmsServer();
            }
            return "停止ems服务器";
        } else if (flag == 1) {
            if (emsServerService.getServer().isStopped()) {
                emsServerService.startEmsServer();
            }
            return "启动ems服务器";
        }
        return "只能输入0或者1,0停止，1启动";
    }

    @GetMapping("/control-ems-yx")
    public Object controlEmsYx(@RequestParam("agc") Integer agcInput, @RequestParam("avc") Integer avcInput) throws IOException {
        emsDataService.updateOrigin("EMSInput", agcInput == 1 ? 1.0f : 0.0f);
        emsDataService.updateOrigin("EMSInputAvc", avcInput == 1 ? 1.0f : 0.0f);
        return "agcInput=" + agcInput + ",avcInput=" + avcInput;
    }

}
